﻿Public Class Utils
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Str"></param>
    ''' <remarks></remarks>
    Public Shared Sub WriteLog(ByVal Str As String)
        Try
            Dim oWrite As IO.StreamWriter
            Dim FilePath As String
            FilePath = System.Configuration.ConfigurationManager.AppSettings("LogPath")

            If IO.File.Exists(FilePath) Then
                oWrite = IO.File.AppendText(FilePath)
            Else
                oWrite = IO.File.CreateText(FilePath)
            End If
            oWrite.Write(Now.ToString() + ":" + Str + vbCrLf)
            oWrite.Close()
        Catch ex As Exception

        End Try
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function CheckValue(ByVal value As Object) As Object
        If value Is Nothing Then
            Return DBNull.Value
        End If
        Return value
    End Function
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function IsInteger(ByVal value As String) As Boolean
        Try
            Integer.Parse(value)
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function IsDouble(ByVal value As String) As Boolean
        Try
            Double.Parse(value)
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="strDateTime"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function IsDateTime(ByVal strDateTime As String) As Boolean
        Try
            DateTime.ParseExact(strDateTime, PublicVariable.DATE_FORMAT, Nothing, System.Globalization.DateTimeStyles.None)
        Catch
            Return False
        End Try
        Return True
    End Function
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="strDateTime"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function ConvertToDate(ByVal strDateTime As String) As DateTime
        Return DateTime.ParseExact(strDateTime, PublicVariable.DATE_FORMAT, Nothing, System.Globalization.DateTimeStyles.None)
    End Function
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="values"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function CalculateStdDev(values As IEnumerable(Of Double)) As Double
        Dim ret As Double = 0
        If values.Count() > 0 Then
            'Compute the Average      
            Dim avg As Double = values.Average()
            'Perform the Sum of (value-avg)_2_2      
            Dim sum As Double = values.Sum(Function(d) Math.Pow(d - avg, 2))
            'Put it all together      
            ret = Math.Sqrt((sum) / (values.Count() - 1))
        End If
        Return ret
    End Function
End Class
